<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>手写Promise resolve,reject,race,all | Zero2key&#39;s Blog</title>
<meta name="description" content="">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="shortcut icon" href="https://zero2key.gitee.io/favicon.ico">
<link rel="stylesheet" href="https://zero2key.gitee.io/styles/main.css">


  
    <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css" />
  

  


<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>

<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />


  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://zero2key.gitee.io">
        <img src="https://zero2key.gitee.io/images/avatar.png" class="site-logo">
        <h1 class="site-title">Zero2key&#39;s Blog</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="/archives" class="site-nav">
            归档
          </a>
        
      
        
          <a href="/tags" class="site-nav">
            标签
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="site-description">
      
    </div>
    <div class="site-footer">
      Powered by <a href="https://github.com/getgridea/gridea" target="_blank" style="color:#fff;">Gridea</a> And <a style="display: inline-block;
    width: 80px;line-height: 12px;font-size: 12px;position: relative;" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" target="_blank">&nbsp;<img style="height: 40px;position: absolute;left:0;top:-15px;" src=""></img></a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">手写Promise resolve,reject,race,all</h2>
            <div class="post-date">2019-11-06</div>
            
            <div class="post-content">
              <h2 id="resolve">resolve</h2>
<pre><code class="language-js">static resolve(value) {
  return new P(resolve =&gt; resolve(value));
}
</code></pre>
<p><code>resolve</code> 就是返回一个新的 <code>resolved Promise</code>。</p>
<h2 id="reject">reject</h2>
<pre><code class="language-js">static reject(reason) {
  return new P((_, reject) =&gt; reject(reason));
}
</code></pre>
<p><code>reject</code> 就是返回一个新的 <code>rejected Promise</code>。</p>
<h2 id="all">all</h2>
<pre><code class="language-js">static all(pArray) {
  if (!Array.isArray(pArray)) {
    throw new Error('Arguments is not Array');
  }
  return new P((resolve, reject) =&gt; {
    const rets = Array(pArray.length);
    let num = 0;
    pArray.forEach((p, index) =&gt;
      P.resolve(p).then(value =&gt; {
        rets[index] = value;
        num++;
        if (num === pArray.length) {
          resolve(rets);
        }
      }, reject)
    );
  });
}
</code></pre>
<p><code>all</code> 稍微复杂一点，就是需要将 <code>pArray</code> 的所有值暂存下来，等所有的 <code>Promise</code> resolved，再将值返回去。</p>
<h2 id="race">race</h2>
<pre><code class="language-js">static race(pArray) {
  if (!Array.isArray(pArray)) {
    throw new Error('Arguments is not Array');
  }
  return new P((resolve, reject) =&gt; {
    pArray.forEach(p =&gt; P.resolve(p).then(resolve, reject));
  });
}
</code></pre>
<p><code>race</code> 就是返回一个 <code>Promise</code>，完成的条件就是传入的 <code>Promise</code> 哪个完成了，就调用 <code>resolve</code>或 <code>reject</code>。</p>

            </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://zero2key.gitee.io/post/bLJYaNbqg/">
                  <h3 class="post-title">
                    手写Promise-1 核心
                  </h3>
                </a>
              </div>
            

            
              
                <div id="gitalk-container" data-aos="fade-in"></div>
              

              
            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>

<script type="application/javascript">

AOS.init();

hljs.initHighlightingOnLoad()

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>


  
  
    <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
    <script>

      var gitalk = new Gitalk({
        clientID: '077888762ae810c37005',
        clientSecret: 'c443e600f8e2687909ccd669db5353cd0839979a',
        repo: 'Zero2key',
        owner: 'Zero2key',
        admin: ['Zero2key'],
        id: location.pathname,      // Ensure uniqueness and length less than 50
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

      gitalk.render('gitalk-container')

    </script>
  

  




  </body>
</html>
